<template>
  <view v-if="visible" class="rating-popup">
    <view class="popup-mask" @click="closePopup"></view>
    <view class="popup-content">
      <view class="popup-title">给店家评分</view>
      <view v-for="(rating, category) in ratings" :key="category" class="rating-item">
        <text>{{ category }}：</text>
        <view class="star-container">
          <uni-icons
            v-for="star in 5"
            :key="star"
            :type="star <= rating ? 'star-filled' : 'star'"
            size="24"
            color="#FFD700"
            @click="setRating(category, star)"
          ></uni-icons>
        </view>
      </view>
      <!-- New input field for comments -->
      <textarea v-model="comments" class="comments-input" placeholder="请输入评价"></textarea>
      <view class="button-container">
        <button class="btn cancel" @click="closePopup">取消</button>
        <button class="btn submit" @click="submitRating">提交评价</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ratings: {
        '店铺评分': 0
      },
      comments: '' // New data property for comments
    }
  },
  methods: {
    setRating(category, rating) {
      this.$set(this.ratings, category, rating)
    },
    closePopup() {
      this.$emit('close')
    },
    submitRating() {
      // Prepare data to submit
      const reviewData = {
        ratings: this.ratings,
        comments: this.comments
      };
      
      // Emit event to parent component
      this.$emit('submit', reviewData);
      this.closePopup();
    }
  }
}
</script>

<style scoped>
.rating-popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
}
.popup-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
}
.popup-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 10rpx;
  padding: 30rpx;
  width: 80%;
  max-width: 600rpx;
}
.popup-title {
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 30rpx;
  text-align: center;
}
.rating-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}
.star-container {
  display: flex;
}
.comments-input {
  width: 100%;
  height: 100px;
  margin-top: 20px;
  padding: 10px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 5px;
}
.button-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 30rpx;
}
.btn {
  padding: 10rpx 30rpx;
  border-radius: 30rpx;
  font-size: 28rpx;
  margin-left: 20rpx;
}
.cancel {
  background-color: #f0f0f0;
  color: #333;
}
.submit {
  background-color: #007aff;
  color: #fff;
  margin-right: 2px;
}
</style>
